package com.sptokentest.service.ServiceImpl;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sptokentest.pj.dto.UserRoleDto;
import com.sptokentest.entity.UserRole;
import com.sptokentest.mapper.UserMapper;
import com.sptokentest.entity.User;
import com.sptokentest.service.RoleService;
import com.sptokentest.service.UserRoleService;
import com.sptokentest.service.UserService;
import com.sptokentest.utils.PageListUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;

//    @Override
//    public SaResult getusers() {
//        List<String> roleList = StpUtil.getRoleList();
//        List<Long> userIdList = new ArrayList<>();
//        if(roleList.get(0).equals("1")){
//            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
//            lqw.ne(User::getId,StpUtil.getLoginId());
//            return SaResult.data(userService.list(lqw));
//        }
//        else if(roleList.get(0).equals("2")){
//            LambdaQueryWrapper<UserRole> lqw1 = new LambdaQueryWrapper<>();
//            lqw1.eq(UserRole::getRoleId,"3");
//            List<UserRole> userRoleList = userRoleService.list(lqw1);
//            for (UserRole userRole:
//                 userRoleList) {
//                userIdList.add(userRole.getUserId());
//            }
//            return SaResult.data(userService.listByIds(userIdList));
//        }
//        return SaResult.error("非管理员无法获得用户信息");
//    }

    @Override
    public SaResult getPages(int page, int pageSize, String name) {
        List<String> permissionList = StpUtil.getPermissionList(StpUtil.getLoginId());
        if(permissionList.contains("system:user:query")){
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.ne(User::getId,StpUtil.getLoginId());
            lqw.like(name!=null,User::getUsername,name);
            LambdaQueryWrapper<UserRole> lqw1 = new LambdaQueryWrapper<>();
            lqw1.ne(UserRole::getUserId,StpUtil.getLoginId());
            List<UserRoleDto> userRoleDtoList = new ArrayList<>();
            List<UserRole> userRoleList = userRoleService.list(lqw1);
            List<User> userList = userService.list(lqw);
            for (User user :
                    userList) {
                UserRoleDto userRoleDto = new UserRoleDto();
                BeanUtils.copyProperties(user, userRoleDto);
                userRoleDtoList.add(userRoleDto);
            }
            for(int i=0;i<userRoleList.size();i++){
                userRoleDtoList.get(i).setRoleId(userRoleList.get(i).getRoleId());
            }
            List<UserRoleDto> userRoleDtoListFina = new ArrayList<>();
            for (UserRoleDto item :
                    userRoleDtoList) {
                item.setRoleName(roleService.getById(item.getRoleId()).getRoleName());
                userRoleDtoListFina.add(item);
            }

            Page pages = PageListUtils.getPages(page, pageSize, userRoleDtoListFina);
            return SaResult.data(pages);
        }

        return SaResult.error("对不起，您没有权限");
    }

    @Override
    public SaResult delete(Long id) {
        if(StpUtil.getPermissionList(StpUtil.getLoginId()).contains("system:user:delete")){
            if(userService.removeById(id)&&userRoleService.removeById(id)){
                return SaResult.ok();
            }
            else{
                return SaResult.error("删除失败");
            }
        }
        return SaResult.error("对不起，您没有权限");
    }

    @Override
    public SaResult addUser(UserRoleDto user) {
        //检查用户名是否唯一
//        LambdaQueryWrapper<User> lqw2 = new LambdaQueryWrapper<>();
//        lqw2.eq(User::getUsername,user.getUsername());
//        if(userService.list(lqw2).size()>=1){
//            return SaResult.error("该用户名已存在");
//        }
        if(StpUtil.getPermissionList(StpUtil.getLoginId()).contains("system:user:add")){
            //首先检验用户名是否存在
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getUsername,user.getUsername());
            if(userService.getOne(lqw)!=null){
                return SaResult.error("用户名已存在");
            }
            user.setCreateBy(Long.valueOf(String.valueOf(StpUtil.getLoginId())));
            user.setCreateTime(DateUtil.date().toString());
            userService.save(user);
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(user.getRoleId());
            userRoleService.save(userRole);
            return SaResult.ok();
        }
        return SaResult.error("对不起，您没有权限");
    }

    @Override
    public SaResult status(User user) {
        if(StpUtil.getPermissionList(StpUtil.getLoginId()).contains("system:user:status")){
            user.setStatus(user.getStatus()==0 ? 1 : 0);
            userService.updateById(user);
            return SaResult.ok();
        }
        return SaResult.error("对不起，您没有权限");
    }

    @Override
    public SaResult editUser(UserRoleDto user) {
        //检查用户名是否唯一
//        LambdaQueryWrapper<User> lqw1 = new LambdaQueryWrapper<>();
//        lqw1.eq(User::getUsername,user.getUsername());
//        if(userService.list(lqw1).size()>1){
//            return SaResult.error("该用户名已存在");
//        }
        //更新user表
        boolean updateUser = userService.updateById(user);
        //更新userRole表
//        LambdaQueryWrapper<UserRole> lqw2 = new LambdaQueryWrapper<>();
//        lqw2.eq(UserRole::getRoleId,user.getRoleId());
        UserRole userRole = new UserRole();
        userRole.setRoleId(user.getRoleId());
        userRole.setUserId(user.getId());
//        boolean updateUserRole = userRoleService.update(userRole, lqw2);
        boolean updateUserRole = userRoleService.updateById(userRole);
        if(updateUser && updateUserRole){
            return SaResult.ok();
        }
        return SaResult.error("修改失败");
    }
}
